﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ServiceSearch;
using BlueCurve.SearchContract;

public partial class Results : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DoSearch(true);
    }


    protected void search_Click(object sender, ImageClickEventArgs e)
    {
        DoSearch(false);
    }


    private void DoSearch(bool byParams)
    {
        // Initialisation d'une nouvelle instance du service de recherche
        RemoteIndexationClient searcher = new RemoteIndexationClient("WSHttpBinding_IRemoteIndexation");

        // Lancement de la recherche
        List<SearchData> results = new List<SearchData>();


        try
        {
            if (byParams)
            {
                results.AddRange(searcher.Search(new SearchQuery()
                {
                    MaxResponsePerPage = Convert.ToInt32(Request.Params["Pagination"]),
                    Query = Request.Params["Query"],
                    TypeOfQuery = (TypeOfRequest)Enum.Parse(typeof(TypeOfRequest), Request.Params["Typeofquery"], true)
                }));
            }
            else
            {
                results.AddRange(searcher.Search(new SearchQuery()
                {
                    MaxResponsePerPage = 10,
                    Query = this.query.Text,
                    TypeOfQuery = TypeOfRequest.Simple
                }));
            }
        }
        catch {  }
        

        if (results.Count > 0)
        {

            results.ForEach(
                delegate(SearchData result)
                {
                    UserControls_ResultPresenter uc = (UserControls_ResultPresenter)LoadControl(typeof(UserControls_ResultPresenter), new object[] { result });
                    if (uc != null)
                    {
                        ((ContentPlaceHolder)Master.FindControl("AffichageResultats")).Controls.Add(uc);
                    }
                });
        }
        else
        {
            ((ContentPlaceHolder)Master.FindControl("AffichageResultats")).Controls.Add(new Literal()
            {
                ID = "Error",
                Text = "Any results for your query"
            });
        }
    }
}
